package comprehensivePractice;

import java.util.Scanner;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/19
 */
public class Practice01 {
    public static void main(String[] args) {
        System.out.println("---------欢迎来到羊村游戏的世界------------"+"\n");
        System.out.println("         ******************");
        System.out.println("         **    游戏开始    **");
        System.out.println("         ******************");
        System.out.println("出拳规则:1.石头2.剪刀3.布");

        while (true) {
            System.out.println("请选择你的角色(1.沸羊羊2.暖羊羊3.美羊羊)");
            Scanner sc = new Scanner(System.in);
            if(chooseRole()){
                System.out.println("游戏开始吗？(y/n)");
                String answer = sc.next();
                if(answer(answer))
                {
                    System.out.println("是否需要下一轮(y/n)");
                    String answer2 = sc.next();
                    if (answer2.equals("n")) {
                        break;
                    } else if (!answer2.equals("y") && !answer2.equals("n")) {
                        System.out.println("输入不合法(y/n)");
                    }
                }
                break;
            }

        }
    }
    public static boolean answer(String answer)
    {
        Scanner sc=new Scanner(System.in);
         if(answer.equals("y") ) {
             while (true) {
                 System.out.println("请出拳：1.石头2.剪刀3.布");
                 int type1 = sc.nextInt();//我方出拳的类型
                 if (type1 < 1 || type1 > 3) {
                     System.out.println("输入的数字有误，请重新输入！");
                     continue;
                 }
                 int type2 = (int) (Math.random() * 3 + 1);//对方出拳类型
                 if (type1 == type2) {
                     System.out.println("结果：平局，加油啊！");
                 } else if ((type1 == 1 && type2 == 2) || (type1 == 2 && type2 == 3) || (type1 == 3 && type2 == 1)) {
                     System.out.println("结果：恭喜你赢了！");
                 } else {
                     System.out.println("结果：啊！你输了！");
                 }
                 return true;
             }
         } else{
            System.out.println("结束游戏，退出程序");
            return false;
        }
    }
    public static boolean chooseRole(){
        Scanner sc=new Scanner(System.in);
        while(true) {
            System.out.println("请选择你的角色：");
            int choice1 = sc.nextInt();
            if(choice1<1 || choice1>3) {
                System.out.println("输入的数字有误，请重新输入！");
                continue;
            }
            break;
        }
        while (true){
            System.out.println("请选择对手角色：");
            int choice2 = sc.nextInt();
            if(choice2<1 || choice2>3) {
                System.out.println("输入的数字有误，请重新输入！");
                continue;
            }
            return true;

        }
    }
}
